#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys,os
import psutil
from time import sleep
 
 
 
'''判断平台'''
def platform_judge():
    # 将平台信息小写返回
    platform=sys.platform.lower()
    return platform
 
 
 
'''将bytes数转换成更加直观的符号显示'''
def bytes2symbols(bytes_value):
 
    '''单位符号'''
    symbols = ('K' ,'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
 
    '''每个单位对应的bytes数的字典,先定义为空'''
    prefix={}
 
    '''要得到这样的{K:1024, M:1024*1024,G:1024**3},进行for循环'''
    for i,s in enumerate(symbols):
        # '''enumerate()为列举函数'''
        # '''i:代表下标'''
        # '''s:代表改下标对应的值'''
 
        # '''取到符号元组的值,作为prfix字典的key,根据key给value进行赋值'''
        prefix[s]=1024**(i+1)
 
    # '''打印得到的对应字典'''
    # print(prefix)
    symbols_value=0
    symbol=''
    # '''循环prefix字典,得到转换值'''
    for key,value in prefix.items():
        if bytes_value >=value :
            symbols_value=bytes_value/value
            symbol=key
        # 如果不满足最小的KB,则以B显示
        elif bytes_value <1024:
            return  '%0.2fB'%bytes_value
        # '''返回转换值(str)'''
    return  '%0.2f%sB'%(symbols_value,symbol)
 
 
'''实时io速率显示'''
def disk_io_message():
    t1 = psutil.disk_io_counters()
    sleep(1)
    '''判断平台,命令清屏'''
    if platform_judge()=='linux':
        os.system('clear')
    else:
        os.system('cls')
    t2 = psutil.disk_io_counters()
    read_bytes = bytes2symbols(t2.read_bytes - t1.read_bytes)
    write_bytes = bytes2symbols(t2.write_bytes - t1.write_bytes)
    # tps
    tps = t2.read_count + t2.write_count - t1.read_count - t1.write_count
    print('读/s:%10s'%read_bytes)
    print('写/s:%10s'%write_bytes)
    print('Tps:%10d'%tps)
 
 
'''磁盘分区信息显示'''
def disk_partitions():
    # 得到磁盘详细信息的列表
    disks=psutil.disk_partitions()
    # 循环列表,打印
    for disk in disks:
        print('%s %6s %10s'%(disk.device,disk.fstype,disk.opts))
 
 
 
if __name__ == '__main__':
 
    # platform=platform_judge()
    # value=bytes2symbols(1025)
    # print(value)
    # print(platform)
    while True:
        disk_io_message()
    # 磁盘分区信息
    # disk_partitions()
 
    # io实时性能信息
    # disk_io_message()